package com.ruoyi.session;

import java.util.Date;

import io.netty.channel.ChannelHandlerContext;
import lombok.Data;

@Data
public class DeviceSession {
    private final String deviceKey;
    private final ChannelHandlerContext ctx;
    private final Date connectionTime;
    private volatile boolean active = true;

    public DeviceSession(String deviceKey, ChannelHandlerContext ctx) {
        this.deviceKey = deviceKey;
        this.ctx = ctx;
        this.connectionTime = new Date();
    }

    public boolean isActive() {
        return active && ctx.channel().isActive();
    }

    public void close() {
        active = false;
        if (ctx.channel().isActive()) {
            ctx.close();
        }
    }

    public ChannelHandlerContext getCtx() {
        return ctx;
    }
}
